package com.dunqi.gpm.cangxi.WeekReport.WeekChek.WeekCheckDetail

import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.Toast

import com.dunqi.gpm.cangxi.R
import com.dunqi.gpm.cangxi.WPConfig
import com.dunqi.gpm.cangxi.WeekReport.Model.WeekChekModel
import com.dunqi.gpm.cangxi.common.base.Data
import com.dunqi.gpm.cangxi.home.adapter.PhotoAdapter
import com.dunqi.gpm.cangxi.home.constance.Constance
import com.foamtrace.photopicker.SelectModel
import com.foamtrace.photopicker.intent.PhotoPickerIntent
import com.foamtrace.photopicker.intent.PhotoPreviewIntent
import kotlinx.android.synthetic.main.fragment_week_check.*
import org.greenrobot.eventbus.EventBus


class WeekCheckDetailFragment : Fragment(), CheckModel.DataResult {

    override fun success(data: Data<*>?) {
        if (data?.resultCode == 1) {
            Toast.makeText(activity, "审核成功", Toast.LENGTH_SHORT).show()
            EventBus.getDefault().post("refresh_week_checklist")
            activity?.finish()
        }
    }

    override fun fail(throwable: Throwable?) {

    }

    private var item: WeekChekModel? = null
    private var flag: Int? = 0    //1 可编辑   2不可编辑
    var model: CheckModel? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            item = arguments!!.getSerializable(ARG_PARAM1) as WeekChekModel?
            flag = arguments!!.getInt(ARG_PARAM2)
        }
        model = CheckModel()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_week_check, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if (flag != CanEdit && flag != LookInfo) {
            throw RuntimeException("Please put 'flag_checktype' from [2,3] ->[WeekCheckDetailFragment.CanEdit,WeekCheckDetailFragment.LookInfo]")
        }
        project_name.text = item?.projectName
        project_plan_week.text = item?.projectReportWeekly?.weeks.toString()
        project_plan_write_time.text = item?.createTime
        project_plan_content.text = item?.planContext
        project_report_content.text = item?.reportContext
        project_duty_units.text = item?.orgName
        project_plan_writer.text = item?.createName

        if (flag == CanEdit) {//可编辑
            rg_result.visibility = View.VISIBLE
            week_report_title.setRightText("保存", {
                save("${item?.projectReportWeekly?.weeklyReportId}", "${item?.projectId}", project_check_opinion.text.toString(), "${if (check_pass.isChecked) 2 else 3}")
            })
        } else if (flag == LookInfo) {//查看
            project_check_opinion.setText(item?.auditRemark)
            project_check_opinion.layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT
            project_check_opinion.isFocusable = false
            if (item !== null) {
                tv_result.visibility = View.VISIBLE
//                reportStatus = 2 ：审批通过
//                reportStatus = 3 ：审批未通过
//                reportStatus = null ：可进行审批
                var status: String = ""
                when (item?.reportStatus) {
                    2 -> status = "审批通过"
                    3 -> status = "审批未通过"
                    null -> status = "未审批"
                }
                tv_result.text = status
                project_check_time.isEnabled = false
            }
        }

        initPhonto()
//        if (flag == 2) {//已经审核
//            check_no_pass.isEnabled = false
//            rg_result.check(R.id.check_pass)
//        }
    }

    companion object {
        private val ARG_PARAM1 = "param1"
        private val ARG_PARAM2 = "param2"
        val CanEdit: Int = 1
        @JvmField
        val LookInfo: Int = 2

        fun newInstance(param1: WeekChekModel?, flag: Int): WeekCheckDetailFragment {
            val fragment = WeekCheckDetailFragment()
            val args = Bundle()
            args.putSerializable(ARG_PARAM1, param1)
            args.putInt(ARG_PARAM2, flag)
            fragment.arguments = args
            return fragment
        }
    }
//    "weeklyReportId：周报告Id
//    projectId：项目id
//    auditRemark：审批备注
//    auditStatus：结果（2：同意，3：不同退回）"

    fun save(weeklyReportId: String, projectId: String, auditRemark: String, auditStatus: String) {
        model?.Auditsave(this@WeekCheckDetailFragment, weeklyReportId, projectId, auditRemark, auditStatus)
    }

    //    var list_photo = ArrayList()
    var list_photo: ArrayList<String> = ArrayList()
    var adapter_photo: PhotoAdapter? = null

    fun initPhonto() {
        var pics = item?.projectReportWeekly?.pics
        if(pics ==null){
            return
        }
        for (item in pics){
            list_photo.add(WPConfig.PicBaseUrl+item.url)
        }
        adapter_photo = PhotoAdapter(activity, list_photo)
        grid_view.adapter = adapter_photo
        grid_view.setOnItemClickListener { parent, view, position, id ->
            val path = list_photo[position]
            val intent = PhotoPreviewIntent(activity)
            intent.setCurrentItem(position) // 当前选中照片的下标
            val paths = list_photo
            intent.setPhotoPaths(paths) // 已选中的照片地址
            startActivityForResult(intent, Constance.REQUEST_PREVIEW_CODE)
        }
    }
}
